package com.guo.todo.usercenter.service.session;

import com.guo.todo.common.consts.LoginConst;
import com.guo.todo.common.credential.Credential;
import com.guo.todo.common.excepiton.CommonException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;

/**
 * @Auther: hotlove_linx
 * @Date: 2023/7/19 22:30
 * @Description:
 */
public abstract class AbstractAAService implements SessionService {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${session.strategy:3}")
    private int loginStrategy;

    @Resource(name = "uuidTokenProvider")
    private TokenProvider tokenProvider;

    public void createSession(Credential credential) throws CommonException {

        try {
            String token = tokenProvider.generateToken();
            credential.setToken(token);

            switch (loginStrategy) {
                case LoginConst.LOGIN_STRATEGY_NOTALIVE:
                    registerSessionIfUserNotAlive(credential);
                    break;
                case LoginConst.LOGIN_STRATEGY_KICKOUT:
                    registerSessionAndKickoutLastUser(credential);
                    break;
                default:
                    registerSessionNormal(credential);
                    break;
            }
        } catch (Exception exception) {
            logger.info("create session: {}", exception.getMessage());
            throw new CommonException("创建token异常，请联系管理员");
        }
    }

    public abstract void registerSessionNormal(Credential credential);

    public abstract void registerSessionIfUserNotAlive(Credential credential);

    public abstract void registerSessionAndKickoutLastUser(Credential credential);
}
